package com.xuan.mail.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuan.mail.R.R;
import com.xuan.mail.domin.Ssh;
import com.xuan.mail.domin.User;
import com.xuan.mail.service.SshService;
import com.xuan.mail.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private SshService sshService;


    //注册邮箱用户
    @PostMapping("/register")
    public String register(@RequestBody User user){
        //输出
        System.out.println(user);
        //验证邮箱是否已经注册
        Ssh ssh1 = sshService.getOne(new LambdaQueryWrapper<Ssh>().eq(Ssh::getId, 1));
        Ssh ssh2 = sshService.getOne(new LambdaQueryWrapper<Ssh>().eq(Ssh::getId, 2));

        boolean status1 =  sshService.createUserAndSetPassword(ssh1,user.getUserName(), user.getUserPass());
        boolean status2 =  sshService.createUserAndSetPassword(ssh2,user.getUserName(), user.getUserPass());
        System.out.printf("=====================================");
        System.out.printf("status1:%s,status2:%s",status1,status2);
        System.out.printf("=====================================");
        if(status1&&status2){
            userService.register(user);
            return "注册成功";
        }else{
            return "注册失败";
        }
    }
    //邮箱用户登录
    @PostMapping ("/login")
    public R login(@RequestBody User user){
        System.out.printf("login=====================================");
        System.out.printf("user:%s",user);
        User userlogin = userService.login(user);
        Date loginDate = DateUtil.date();
        userService.update(userlogin,new LambdaUpdateWrapper<User>()
                .eq(User::getUserId,userlogin.getUserId())
                .set(User::getLoginDate,loginDate)

        );
        return R.ok(userlogin);

    }
    //修改邮箱密码
    @PostMapping("/changePassword")
    public String changePassword(@RequestBody User user){
        System.out.printf("changePassword=====================================");
        System.out.printf("user:%s",user);

        Ssh ssh1 = sshService.getOne(new LambdaQueryWrapper<Ssh>().eq(Ssh::getId, 1));
        Ssh ssh2 = sshService.getOne(new LambdaQueryWrapper<Ssh>().eq(Ssh::getId, 2));
        boolean status1 =  sshService.changeUserPassword(ssh1,user.getUserName(), user.getUserPass());
        boolean status2 =  sshService.changeUserPassword(ssh2,user.getUserName(), user.getUserPass());
        if(status1&&status2){
            userService.changePassword(user);
            return "密码修改成功";
        }else{
            return "密码修改失败";
        }
    }
    //用户注销
    @PostMapping("/deleteUser")
    public String deleteUser(@RequestBody User user){
        System.out.printf("deleteUser=====================================");
        System.out.printf("user:%s",user);

        Ssh ssh1 = sshService.getOne(new LambdaQueryWrapper<Ssh>().eq(Ssh::getId, 1));
        Ssh ssh2 = sshService.getOne(new LambdaQueryWrapper<Ssh>().eq(Ssh::getId, 2));
        boolean status1 =  sshService.deleteUser(ssh1,user.getUserName());
        boolean status2 =  sshService.deleteUser(ssh2,user.getUserName());
        if(status1&&status2){
            userService.logicDeleteUser(user);
            return "注销成功";
        }else{
            return "注销失败";
        }
    }

    //用户分页
    @GetMapping("/list")
    public Page<User> getUserPage(@RequestParam String key,
                                  @RequestParam(defaultValue = "1")Integer pageNum,
                                  @RequestParam(defaultValue = "10")Integer pageSize){
        Page<User> userPage = userService.getUserPage(key, pageNum,pageSize);
        System.out.println("page记录"+userPage.getRecords());
        System.out.println("当前页码：" + userPage.getCurrent());
        System.out.println("每页记录数：" + userPage.getSize());
        System.out.println("总记录数：" + userPage.getTotal());
        System.out.println("总页数：" + userPage.getPages());
        System.out.println("是否有上一页：" + userPage.hasPrevious());
        System.out.println("是否有下一页：" + userPage.hasNext());
        return userPage;
    }
    //封禁用户
    @PostMapping("/banUser")
    public String banUser(@RequestBody User user){
        User user1 = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUserId, user.getUserId()));
        userService.banUser(user1);

        return "封禁成功";
    }
    //解禁用户
    @PostMapping("/unBanUser")
    public String unBanUser(@RequestBody User user){
        User user1 = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUserId, user.getUserId()));
        userService.unBanUser(user1);
        return "解禁成功";
    }
    //忘记密码
    @PostMapping("/forgetPassword")
    public String forgetPassword(@RequestParam String userName){
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUserName, userName));
        if(user==null){
            return "用户不存在";
        }
        userService.forgetPassword(user);

        return "忘记密码";
    }
    //重置密码
    @PostMapping("/resetPassword")
    public String resetPassword(@RequestBody User user) {

        userService.resetPassword(user);

        return "重置密码成功";

    }


}
